package com.example.demo.util;
/**
 * 包含数据的返回结果
 *
 */
public class DataResult<T> extends BaseResult {

    private static final long serialVersionUID = 5173565130912467477L;

    /** 业务数据对象（可以为空） */
    private T       data;
    private int     totals;
    private Integer limit;
    private String  marker;

    public DataResult(String code, String message, Boolean success) {
        super(code, message, success);
    }

    public DataResult(String code, String message, Boolean success, T data) {
        super(code, message, success);
        this.data = data;
    }

    public DataResult(String code, String message, Boolean success, T data, String marker,
                      Integer limit) {
        super(code, message, success);
        this.data = data;
        this.marker = marker;
        this.limit = limit;
    }
    
    public DataResult(String code, String message, Boolean success, T data, int totals) {
        super(code, message, success);
        this.data = data;
        this.totals = totals;
    }

    public static class DataResultBuilder<U> {

        private BaseResultBuilder baseResultBuilder = BaseResultBuilder.newBuilder();
        private U                 data;

        public static DataResultBuilder<Object> newBuilder() {
            return new DataResultBuilder<>();
        }

        public DataResultBuilder<U> code(String code) {
            this.baseResultBuilder.code(code);
            return this;
        }

        public DataResultBuilder<U> message(String message) {
            this.baseResultBuilder.message(message);
            return this;
        }

        public DataResultBuilder<U> data(U data) {
            this.data = data;
            return this;
        }

        public <V extends U> DataResult<V> build() {
            return new DataResult<>(this);
        }
    }

    public DataResult() {
    }

    private DataResult(DataResultBuilder<? super T> builder) {
        super(builder.baseResultBuilder);
        //noinspection unchecked
        this.data = (T) builder.data;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getTotals() {
        return totals;
    }

    public void setTotals(int totals) {
        this.totals = totals;
    }

    @Override
    public String toString() {
        return "DataResult{" + "code=" + getCode() + ", message='" + getMessage() + '\'' + ", data="
               + data + '}';
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public String getMarker() {
        return marker;
    }

    public void setMarker(String marker) {
        this.marker = marker;
    }
}
